#!/bin/bash
##K8S安装部署脚本
install_k8s_sysctl_config(){
    cat <<EOF |  tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
    swapoff -a
    sed -i 's/.*swap.*/#&/' /etc/fstab
}
install_k8s_install_k8s(){
	service_install  'kubelet kubectl kubeadm'
	chek_status "$?" "K8S安装"
	systemctl enable kubelet 
}

install_k8s_install_etcd(){
	service_install 'etcd'
}
install_k8s_install_bash_completion(){
	service_install "bash-completion"
	chek_status "$?" "自动补全工具"
	source /usr/share/bash-completion/bash_completion
	auto="kubectl kubeadm"
	for i in $auto;do
		source <($i completion bash)
		echo "source <($i completion bash)" >> ~/.bashrc
	done
}

install_k8s_config_init(){
	tools_k8s__images_pull "$HOST_IP"
	if [[ "$pull" == 'n' ]];then
		green_zt '跳过镜像拉取'
	else
		if [[ -e 'init/image_calico.sh' ]];then
			bash /init/image_calico.sh
		fi
	fi
	if [[ "$node_type" == "node" ]];then
		if [[ "$mirror_pull" == 'y' ]];then
			tools_Dockr_tools_Docker_Private_mirrors "$Master_IP"
			green_zt "请输入: [ yes ]，然后输入该节点[ $Master_IP ]的密码"
			scp ${Master_IP}:/images_master_pull.sh /images_master_pull.sh
			if [[ -e "/images_master_pull.sh" ]];then
				bash /images_master_pull.sh
			fi
		fi
		red_z_green_z "请在Master节点执行下面的命令:\n" "[ kubeadm token create --print-join-command ]"
		exit 0
	elif [[ "$node_type" == "master" ]];then
		rm -f /images_master_pull.sh
		tools_k8s__images_push_config "$Master_IP"
		image_s=`docker images | grep -v 'IMAGE ID' | awk '{print $1,$2}' | sed 's/ /:/g' | grep 'k8s.gcr.io' | wc -l`
		for((i=1;i<=$image_s;i++));do
			new=`docker images | grep -v 'IMAGE ID' | awk '{print $1,$2}' | sed -e 's/ /:/g' -e "s/k8s.gcr.io/${IP}:5000/g" | sed -n ${i}p | grep -v '<none>'`
			old=`docker images | grep -v 'IMAGE ID' | awk '{print $1,$2}' | sed 's/ /:/g' | grep 'k8s.gcr.io' | sed -n ${i}p | grep -v '<none>'`
			docker tag $old $new
			echo -e "docker pull $new" >> /k8s_base_images_pull.sh
			if [[ -n "$new" ]];then
				if [[ "$registry_push" == 'y' ]];then
					if [[ `echo $new | awk -F ':' '{print $3}'` == '<none>' ]];then
						green_zt "跳过: [ $new ]"
					else
						docker push $new
						echo -e "docker pull $new" >> /images_master_pull.sh						
					fi
				fi
			fi
		done
		red_z_green_z  "正在执行初始化命令-------->" '[ bash /init/k8s.sh ]'
		su -c 'bash /init/k8s.sh'
		green_zt "永远相信美好的事情即将发生"
		exit 0
	else
		green_zt "永远相信美好的事情即将发生"
	fi
}
install_k8s_config_node(){
	input "请输入Node节点数量" "node_s"
	rm -f info.conf
	if [[ -z "$node_s" || "$node_s" == "0" ]];then
		green_zt "本次未设置Node节点"
	else
		for((i=1;i<=$node_s;i++));do
			input "请输入第${i}个节点主机名" "n_n"
			input "请输入${n_n}节点IP地址[不做网络连通性检测]" "n_ip"
			echo -e "${n_ip} ${n_n}" >> /etc/hosts 
			echo -e "${n_ip} ${n_n}" >> info.conf
		done
	fi
}
#判断当前主机信息
install_k8s_config_installer(){
	input "是否初始化主机名映射文件[y/n],默认[ n ]" "inits"
	if [[ "$inits" == 'y' ]];then
		green_zt "保留现有hosts文件"
	else
		if [[ "$Package_managements" == 'yum' ]];then
			echo -e "127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
" > /etc/hosts
		# elif [[  "$Package_managements" == 'apt' ]];then
		fi
	fi
	#判断当前主机是否可以作为Master节点
	cpus=`cat /proc/cpuinfo| grep "processor"| sort| uniq| wc -l`
	if [[ "$cpus" -lt "2" ]];then
		red_z_green_z_info "本机CPU核心数量无法满足Master最低要求[最低2核]\n" "已默认设置本节点为Node节点"
	else
		input "请选择本机节点类型\n[1]Master\n[2]Node [Default]" t
		input_check "$t" "本机节点类型" "2" types
	fi
	input "先拉取镜像再配置服务:[y/n];默认[y]" "pull"
	config_openstack_installer_default_conf #获取本机IP地址
	if [[ "$types" == '1' ]];then
		node_type='master'
		input "请设置此节点主机名,默认 [ master ]" Master_NameS
		input_check "$Master_NameS" "此节点主机名设置" 'master' master_name
		hostnamectl set-hostname "$master_name" 


		input "请设置本机IP地址用于配置集群,默认: [ $HOST_IP ]" 'Master_IP'
		input_check "$Master_IP" "本机IP地址选择" "$HOST_IP" 'Master_IP'

		input "是否创建并推送镜像私有镜像仓库[ y/n ],默认[ y ]" registry_push
		input_check "$registry_push" "创建并推送镜像私有镜像仓库" "y" registry_push


		red_z_green_z	"Matser节点主机名:"	" [ $master_name ]"
		red_z_green_z	"Matser节点IP地址:"	"[ $Master_IP ]"
		red_z_green_z	"创建并推送镜像私有镜像仓库:"	"[ $mirror_pull ]"
		input '按回车开始安装---->>' i_dd

		echo -e "$Master_IP $master_name" >> /etc/hosts
		mkdir -p /init/
		rm -f /init/calico.yaml /init/k8s.sh
		cp ./config/calico.yaml /init/
		cp ./config/k8s.sh /init/
		sed -i "s#HOST_IP#$HOST_IP#" /init/k8s.sh
		install_k8s_config_node
	else
		node_type='node'
		input "请设置Matser节点主机名,默认[ master ]" "Master_NameS"
		input_check "$Master_NameS" "Matser节点主机名" 'master' master_name

		input "请设置Matser节点IP地址,默认[ $NODE_HOST_IP ]" "Master_IP"
		input_check "$Master_IP" "Matser节点主机名" "$NODE_HOST_IP" 'Master_IP'

		input "请设置本机主机名,默认[ node ]" 'Node_Name'
		input_check "$Node_Name" "本机主机名设置" "node" "Node_Name"
		hostnamectl set-hostname $Node_Name

		input "请设置当前节点IP地址,默认[ $HOST_IP ]" "local_ip"
		input_check "$local_ip" "Matser节点主机名" "$HOST_IP" 'local_ip'
		echo -e "$Master_IP $master_name\n$local_ip $Node_Name" >> /etc/hosts

		input "是否从私有镜像仓库拉取镜像[ y/n],默认[ y ]" 'mirror_pull'
		input_check "$mirror_pull" "从私有镜像仓库拉取镜像" "y" mirror_pull
		red_z_green_z	"Matser节点主机名:"	" [ $master_name ]"
		red_z_green_z	"Matser节点IP地址:"	"[ $Master_IP ]"
		red_z_green_z	"设置本机主机名为:"	" [ $Node_Name ]"
		red_z_green_z	"设置当前节点IP地址:" " [ $local_ip ]"
		red_z_green_z	"从私有镜像仓库拉取镜像:"	"[ $mirror_pull ]"
		input '按回车开始安装---->>' i_dd
	fi
}
install_k8s_all(){
	#配置安装环境
	install_k8s_sysctl_config
	#配置安装前信息
	install_k8s_config_installer
	#安装K8S
	install_k8s_install_k8s
	#镜像拉取

	#初始化K8S
	install_k8s_config_init
}